package com.panshi.controller;

import com.panshi.entity.AppMatterT;
import com.panshi.entity.AppUserT;
import com.panshi.entity.ClassifyT;
import com.panshi.entity.SysAnswerCheckT;
import com.panshi.result.R;
import com.panshi.service.AnswerClientService;
import com.panshi.service.QaClientService;
import com.panshi.service.UserClientService;
import com.panshi.service.ZxClientService;
import com.panshi.util.BaseUpload;
import com.panshi.util.QaUtils;
import com.panshi.util.UserInfoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

@Controller
public class AnswersController {

    @Autowired
    private UserInfoUtils userInfoUtils;

    //咨询ID
    @Autowired
    private ZxClientService zxClientService;

    //问答ID
    @Autowired
    private QaClientService qaClientService;

    //回答ID
    @Autowired
    private AnswerClientService answerClientService;

    //
    @Autowired
    private QaUtils qaUtils;

    @Autowired
    private BaseUpload baseUpload;

    //
    @Autowired
    private UserClientService userClientService;






    @PostMapping("/qaAddReply")
    @ResponseBody
    public R qaAddReply(HttpServletRequest request,HttpServletResponse response) throws Exception {

        R r = new R();
        AppUserT getuser = userInfoUtils.getuser(request, response);
        if(getuser==null){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("登录失效，请重新登录");
            return r;
        }
        System.out.println("问答添加");
        String matterId = request.getParameter("matterId");
        String content = request.getParameter("content");
        String essayimg = request.getParameter("essayimg");
        if(matterId==null || content == null){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("异常评论");
            return r;
        }
        //回复审核--------》
        //图片路径

        System.out.println(essayimg);


        SysAnswerCheckT sysAnswerCheckT = new SysAnswerCheckT();
        sysAnswerCheckT.setUserId(getuser.getUserId());
        sysAnswerCheckT.setAnswerContent(content);
        if(essayimg!=null){
            String s = baseUpload.put64myImage(essayimg);
            sysAnswerCheckT.setImagePath(s);
        }
        System.out.println(sysAnswerCheckT);
        Boolean aBoolean = answerClientService.answerAdd(sysAnswerCheckT);
        if(aBoolean){
            r.setCode(201);
            r.setSuccess(aBoolean);
            r.setMessage("评论成功，待审核！！！！！！");
            return r;
        }
        return R.error();
    }




    /**
     * 主页
     * @param model
     * @return
     */
    @GetMapping("/answers_main/{cid}")
    public String answersMain(@PathVariable int cid, HttpServletRequest request, Model model, HttpServletResponse response){
        model.addAttribute("wd","wd");

        System.out.println("问答     初始化数据---------》");
        model.addAttribute("classId",cid);
        List<ClassifyT> qaClassifyTList = qaClientService.zall();
        //咨询分类列表
        model.addAttribute("qaClassifyTList",qaClassifyTList);
        //咨询分类列表
        model.addAttribute("zxClassifyTList",qaClassifyTList);

        //咨询信息
        List<AppMatterT> qaMatters = qaUtils.qaList(cid, 1, 7, "read_count");
        Collections.shuffle(qaMatters);
        model.addAttribute("qaAppMatterTList",qaMatters);

        //大家都在问
        List<AppMatterT> allQaMatters = qaUtils.qaList(cid, 1, 10, "answer_count");
        Collections.shuffle(allQaMatters);
        model.addAttribute("allQaMatters",allQaMatters);

        //用户
        userInfoUtils.veruser(request,model,response);

        return "answers/answers_main";
    }
    //按需查询
    @PostMapping("/qaClickType")
    @ResponseBody
    public R qaClickType(HttpServletRequest request){
        String cid = request.getParameter("cid");
        if(cid==null){
            cid="0";
        }
        String type = request.getParameter("type");
        if(type==null){
            type="10";
        }
        String limit = request.getParameter("limit");
        String page = request.getParameter("page");
        R r = new R();
        System.out.println(type+" "+limit+" "+cid+" "+page);
        List<AppMatterT> appMatterTS = qaUtils.qaClickType(Integer.valueOf(cid), Integer.valueOf(page), Integer.valueOf(limit), type);
        if(appMatterTS.isEmpty()){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("没有数据了");
            return r;
        }
        String data = qaUtils.joinData(appMatterTS);
        r.setCode(201);
        r.setSuccess(true);
        r.setMessage("查询成功");
        r.setData(data);
        return r;
    }



    /**
     * 问答详情bymid
     */
    @GetMapping("/answers_ptl/{id}")
    public String answersParticulars(HttpServletRequest request,Model model,@PathVariable String id,HttpServletResponse response){
        model.addAttribute("wd","wd");
        System.out.println(id);
        userInfoUtils.veruser(request,model,response);
        if(id==null){
            return "/error/404";
        }
        System.out.println("问答详情数据---------》"+id);

        List<ClassifyT> qaClassifyTList = qaClientService.zall();
        //咨询分类列表
        model.addAttribute("qaClassifyTList",qaClassifyTList);

        AppMatterT appMatterT = qaClientService.matById(Integer.valueOf(id));
        if(appMatterT==null || appMatterT.getUserId()==null){
            return "/error/404";
        }
        model.addAttribute("classId",appMatterT.getClassifyId());
        if(appMatterT!=null){
            //获取问答分类
            appMatterT.setClassifyT(qaClientService.bycid(appMatterT.getClassifyId()));
            //获取答题对象
            appMatterT.setAnswerT(answerClientService.bymid(appMatterT.getMatterId()));
            //获取用户对象
            appMatterT.setAppUserT(userClientService.byid(appMatterT.getUserId()));
            //回答用户对象
            if(appMatterT.getAnswerT()!=null){
                appMatterT.setReplyUser(userClientService.byid(appMatterT.getAnswerT().getUserId()));
            }
            List<AppMatterT> answerRe = qaClientService.qaPage(appMatterT.getClassifyId(), 1, 5, "answer_count");
            Collections.shuffle(answerRe);
            model.addAttribute("answerRe",answerRe);

            List<AppMatterT> groundMatters = qaClientService.qaPage(appMatterT.getClassifyId(), 1, 5, "ground_time");
            Collections.shuffle(groundMatters);
            model.addAttribute("groundMatters",groundMatters);
        }
        model.addAttribute("appMatterT",appMatterT);

        //相关问题  --- 》

        userInfoUtils.veruser(request,model,response);

        //增加浏览
        qaClientService.matterReadAdd(appMatterT.getMatterId());

        return "answers/answers_particulars";
    }




}
